package com.educatioin.commons.http;

import com.educatioin.commons.http.dto.Request;
import com.educatioin.commons.http.dto.Response;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.InputStreamReader;
import java.io.Reader;

/**
 * http客户端
 * @author chenr
 */
public class HttpClient<T> {
    /**
     * do get
     * @return
     */
    public Response<T> doGost(String url, Request<Object> request, TypeReference returnType) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列
        try {
            ObjectMapper mapper = new ObjectMapper();
            StringEntity s = new StringEntity(mapper.writeValueAsString(request));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            httppost.setEntity(s);
            HttpResponse res = httpclient.execute(httppost);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String charset = EntityUtils.getContentCharSet(entity);
                Reader reader = new InputStreamReader(entity.getContent(), charset);
                return mapper.readValue(reader, returnType);
            } else {
                Response<T> rsp = new Response<>();
                rsp.setCode(Response.Fail_CODE);
                return rsp;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * do post
     * @return
     */
    public Response<T> findItemWithDetail(String url, Request<Object> request, TypeReference returnType) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列
        try {
            ObjectMapper mapper = new ObjectMapper();
            StringEntity s = new StringEntity(mapper.writeValueAsString(request));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            httppost.setEntity(s);

            HttpResponse res = httpclient.execute(httppost);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String charset = EntityUtils.getContentCharSet(entity);
                Reader reader = new InputStreamReader(entity.getContent(), charset);
//                return mapper.readValue(reader, new TypeReference<Response<Map<Long, ItemInfoDTO>>>() {});
                return mapper.readValue(reader, returnType);
            } else {
                Response<T> rsp = new Response<>();
                rsp.setCode(Response.Fail_CODE);
                return rsp;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
